import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/ui/ui.dart';

class UserScopeSwitch extends StatefulWidget {
  const UserScopeSwitch({
    super.key,
    this.scope = 1,
    this.onChanged,
  });
  final int scope;
  final Function(int scope)? onChanged;
  @override
  State<UserScopeSwitch> createState() => _UserScopeSwitchState();
}

class _UserScopeSwitchState extends State<UserScopeSwitch> {
  Radius radius = const Radius.circular(UiTheme.boxRadius);
  EdgeInsets padding = const EdgeInsets.all(13);
  int scope = 0;
  @override
  void initState() {
    setState(() {
      scope = widget.scope;
    });
    super.initState();
  }

  getBgColor(i) {
    return i == scope ? Colors.white : Colors.grey[100];
  }

  getTextStyle(i) {
    return TextStyle(
      color: i == scope ? UiTheme.primary : null,
      fontWeight: i == scope ? FontWeight.bold : null,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: UiTap(
            onTap: () {
              setState(() {
                scope = 1;
              });
              widget.onChanged?.call(scope);
            },
            child: Container(
              decoration: BoxDecoration(
                color: getBgColor(2),
                borderRadius: BorderRadius.only(topLeft: radius),
              ),
              child: Container(
                padding: padding,
                decoration: BoxDecoration(
                  color: getBgColor(1),
                  borderRadius: BorderRadius.only(
                    topLeft: radius,
                    topRight: scope == 1 ? radius : Radius.zero,
                    bottomRight: scope == 1 ? Radius.zero : radius,
                  ),
                ),
                alignment: Alignment.center,
                child: Text(
                  '自然人',
                  style: getTextStyle(1),
                ),
              ),
            ),
          ),
        ),
        Expanded(
          child: UiTap(
            onTap: () {
              setState(() {
                scope = 2;
              });
              widget.onChanged?.call(scope);
            },
            child: Container(
              decoration: BoxDecoration(
                color: getBgColor(1),
                borderRadius: BorderRadius.only(topRight: radius),
              ),
              child: Container(
                padding: padding,
                decoration: BoxDecoration(
                  color: getBgColor(2),
                  borderRadius: BorderRadius.only(
                    topLeft: scope == 2 ? radius : Radius.zero,
                    topRight: radius,
                    bottomLeft: scope == 2 ? Radius.zero : radius,
                  ),
                ),
                alignment: Alignment.center,
                child: Text(
                  '法人',
                  style: getTextStyle(2),
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
